<?php
class Sprint extends CI_Controller {

	function __construct(){
        parent::__construct();
        
        $this->load->model('sprint_model');
        $this->load->helper(array('form','url'));
        $this->load->library('form_validation');
    }
    function index(){
        $this->load->view('sprintsList');
    } 
    
	function getSprints(){
		$page = $this->input->post('page');
		$limit = $this->input->post('rows');
		$sidx = $this->input->post('sidx');
		$sord = $this->input->post('sord');
		
		if(!$sidx) $sidx =1;
		
		$count = $this->sprint_model->getSprintsCount();

		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages) $page=$total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
		
		$responce->page = $page;
		$responce->total = $total_pages;
		$responce->records = $count;
		
		$sprints = $this->sprint_model->getSprints($sidx, $sord, $start , $limit); 
		
		for($i=0; $i< sizeof($sprints); $i++) {
			$responce->rows[$i]['id'] = $sprints[$i]['id'];
			$responce->rows[$i]['cell'] = array($sprints[$i]['id'], 
												$sprints[$i]['start_date'], 
												$sprints[$i]['end_date'],
												$sprints[$i]['status']);
		}
     	
		echo json_encode($responce);
	}
	
	function save(){
		$this->load->model('sprint_model');
		$project_id		= $this->session->userdata('current_project');
		$id 			= $this->input->post('id');
		$d1				=$_POST['dateIni'];
		$d2				=$_POST['dateEnd'];
		$dateIni 		= date('Y-m-d', strtotime($d1));
		$dateEnd 		= date('Y-m-d', strtotime($d2));
		$status 		= $_POST['status_sprint'];
		$name 			= date($d1 . ' - ' . $d2);
		if($this->sprint_model->controlInProgress($status,$id)==true){
			//cannot be more than one sprint in progress
			redirect('/index.php/authentication_controller/main_view/4' );
			return;
		}
		if($id != NULL && $id != ''){
			$this->sprint_model->update($id,$dateIni,$dateEnd,$name,$status);
		}
		else{
			if($status == 'Closed'){
				//cannot create a closed sprint
				redirect('/index.php/authentication_controller/main_view/4' );
				return;
			}
			
			$this->sprint_model->create($dateIni,$dateEnd,$name,$status,$project_id);
		}
		redirect('/index.php/authentication_controller/main_view/4' );
	}
    
	public function get_json_sprints_autocomplete(){
		$term = $this->input->get('term');
		$sprints = $this->sprint_model->get_like_sprints($term);
		
		$auto_complete_sprints = array();
		
		for($i=0; $i< sizeof($sprints); $i++) {
			$auto_complete_sprints[$i]['id'] 	= $sprints[$i]['id'];
			$auto_complete_sprints[$i]['value'] = $sprints[$i]['name'];
		}
		
		echo json_encode($auto_complete_sprints);
	}
	
	function get(){
		$id = $_POST['id'];
		$result = NULL;
		
		if($id != NULL && $id != ''){
			$sprint = $this->sprint_model->get($id);
			$result = $sprint[0];
		}
		echo json_encode($result);
	}
 
	public function current_sprint(){
		$c_sprint 	= $this->sprint_model->get_current_sprint();
		$tasks		= $this->get_sprint_tasks($c_sprint['id']);
		
		$pend_tasks = 0;
		$prog_tasks	= 0;
		$comp_tasks	= 0;

		$est_hours	= 0;
		
		// Retrieve sprint duration
		$start_date = new DateTime($c_sprint['start_date']);
		$end_date 	= new DateTime($c_sprint['end_date']);
		$duration 	= $start_date->diff($end_date)->format('%d') + 1;

		if($tasks != NULL){
			foreach ($tasks as $t){
				// Get tasks status
				switch ($t['status']) {
					case 0: $pend_tasks++; break;
					case 1: $prog_tasks++; break;
					case 2: $comp_tasks++; break;
				}
				
				// Get estimated hours
				$est_hours += $t['estimated_hours'];
			}
		}
		
		$data = array(
			'duration'		=> $duration,
			'start_date' 	=> $start_date->format('m/d/Y'),
			'end_date'		=> $end_date->format('m/d/Y'),
			'size_tasks' 	=> sizeof($tasks),
			'pend_tasks'	=> $pend_tasks,
			'prog_tasks'	=> $prog_tasks,
			'comp_tasks'	=> $comp_tasks
		);
		redirect('/index.php/authentication_controller/main_view/'+ $data );
		//$this->parser->parse('current_sprint', $data);
		//	$this->load->view('current_sprint', $c_sprint);
		
		}
		
	public function get_sprint_tasks($sprint_id){
		$this->load->model('task');
		$s_tasks = $this->task->get_tasks_by_sprint($sprint_id);
		return $s_tasks; 
	}
	
	public function get_full_sprint(){
		$id = $this->input->get('id');
		$tasks = $this->get_sprint_tasks($id);
		
		$pend_tasks = 0;
		$prog_tasks	= 0;
		$comp_tasks	= 0;

		$est_hours	= 0;
		
		// Retrieve sprint duration
		$start_date = new DateTime($c_sprint['start_date']);
		$end_date 	= new DateTime($c_sprint['end_date']);
		$duration 	= $start_date->diff($end_date)->format('%d') + 1;

		if($tasks != NULL){
			foreach ($tasks as $t){
				// Get tasks status
				switch ($t['status']) {
					case 0: $pend_tasks++; break;
					case 1: $prog_tasks++; break;
					case 2: $comp_tasks++; break;
				}
				
				// Get estimated hours
				$est_hours += $t['estimated_hours'];
			}
		}
		
		$data = array(
			'duration'		=> $duration,
			'start_date' 	=> $start_date->format('m/d/Y'),
			'end_date'		=> $end_date->format('m/d/Y'),
			'size_tasks' 	=> sizeof($tasks),
			'pend_tasks'	=> $pend_tasks,
			'prog_tasks'	=> $prog_tasks,
			'comp_tasks'	=> $comp_tasks,
			'tasks'			=> $tasks
		);
		
		
		
	}
	
}
?>